package com.zjut.zs.applyview.manager;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.zjut.an.config.dao.AnConfig;
import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.file.dao.AnFile;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Status;
import com.zjut.zs.applyview.dao.ZsApplyView;
import com.zjut.zs.applyview.dao.ZsApplyViewDAO;
import com.zjut.zs.cj.dao.ZsCj;
import com.zjut.zs.cj.manager.ZsCjManager;
import com.zjut.zs.exvalue.dao.ZsExValue;
import com.zjut.zs.exvalue.manager.ZsExValueManager;
import com.zjut.zs.file.dao.ZsFile;
import com.zjut.zs.file.manager.ZsFileManager;
import com.zjut.zs.school.dao.ZsSchool;
import com.zjut.zs.school.manager.ZsSchoolManager;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.user.manager.ZsUserManager;
import com.zjut.zs.userinfo.dao.ZsUserinfo;
import com.zjut.zs.userinfo.manager.ZsUserinfoManager;
import com.zjut.zs.userstatus.manager.ZsUserStatusManager;
import com.zjut.zs.util.tools.DataCount;
import com.zjut.zs.xy.dao.ZsXy;
import com.zjut.zs.xy.manager.ZsXyManager;


public class ZsApplyViewManager {

	@Resource(name = "ZsApplyViewDAO")
	private ZsApplyViewDAO dao;	

	@Resource(name = "AnConfigManager")
	private AnConfigManager anconfig;

	@Resource(name = "ZsSchoolManager")
	private ZsSchoolManager zsManager;

	@Resource(name = "ZsCjManager")
	private ZsCjManager cjmanager;

	@Resource(name = "ZsXyManager")
	private ZsXyManager xymanager;

	@Resource(name = "ZsSchoolManager")
	private ZsSchoolManager scManager;

	@Resource(name = "ZsUserManager")
	private ZsUserManager umanager;

	@Resource(name = "ZsUserinfoManager")
	private ZsUserinfoManager ufmanager;

	@Resource(name = "ZsUserStatusManager")
	private ZsUserStatusManager smanager;

	@Resource(name = "ZsExValueManager")
	private ZsExValueManager exmanager;

	@Resource(name = "ZsFileManager")
	private ZsFileManager fmanager;

	public PageModule getDataByInstance (int pageNumber,int pageSize, String sort, String order, ZsApplyView obj) {
		return dao.findByInstance(pageNumber, pageSize, sort, order, obj);
	}

	public int getCountByInstance (ZsApplyView obj) {
		return dao.countByInstance(obj);
	}

	public List getDatas() {
		return dao.findAll();
	}

	public ZsApplyView getData(String usId) {
		return dao.findById(usId);
	}

	public PageModule getDataByAB(int pageNumber, int pageSize, String sort, String order, Integer anum, Integer bnum) {
		String hql = "from ZsApplyView where 1=1";//已初审中
		if (null!=anum) {
			hql+=" and ((CASE WHEN inYwdd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inWydd = 'A' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSxdd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inZzdd = 'A' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inLsdd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inDldd = 'A' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inWldd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inHxdd = 'A' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSwdd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inJsdd = 'A' THEN 1 ELSE 0 END)) >=  "+anum+"  ";
		}
		if (null!=bnum) {
			hql+=" and ((CASE WHEN inYwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inWydd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSxdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inZzdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inLsdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inDldd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inWldd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inHxdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inJsdd = 'B' THEN 1 ELSE 0 END)) >=  "+bnum+"  ";
		}
		if (null!=sort&& null!=order) {
			hql+=" order by "+ sort + " "+order;
		}

		List objects = dao.findByHql(pageNumber, pageSize, hql, null);
		return (new PageModule(pageNumber,pageSize,this.getDataCountByAB(anum, bnum),objects));
	}

	public int getDataCountByAB(Integer anum, Integer bnum) {
		String chql = "select count(*) from ZsApplyView where  1=1";
		if (null!=anum) {
			chql+=" and ((CASE WHEN inYwdd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inWydd = 'A' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSxdd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inZzdd = 'A' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inLsdd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inDldd = 'A' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inWldd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inHxdd = 'A' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSwdd = 'A' THEN 1 ELSE 0 END) + (CASE WHEN inJsdd = 'A' THEN 1 ELSE 0 END)) >=  "+anum+"  ";
		}
		if (null!=bnum) {
			chql+=" and ((CASE WHEN inYwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inWydd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSxdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inZzdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inLsdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inDldd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inWldd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inHxdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inJsdd = 'B' THEN 1 ELSE 0 END)) >=  "+bnum+"  ";
		}
		int count = dao.findCountByHql(chql, null);
		return count;
	}

	public PageModule getDataByAorB(int pageNumber, int pageSize, String sort, String order, Integer num) {
		String hql = "from ZsApplyView where 1=1";//已初审中
		if (null!=num) {
			hql+=" and ((CASE WHEN inYwdd = 'A' or inYwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inWydd = 'A' or inWydd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSxdd = 'A' or inSxdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inZzdd = 'A' or inZzdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inLsdd = 'A' or inLsdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inDldd = 'A' or inDldd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inWldd = 'A' or inWldd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inHxdd = 'A' or inHxdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSwdd = 'A' or inSwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inJsdd = 'A' or inJsdd = 'B' THEN 1 ELSE 0 END)) >=  "+num+"  ";
		}

		if (null!=sort&& null!=order) {
			hql+=" order by "+ sort + " "+order;
		}

		List objects = dao.findByHql(pageNumber, pageSize, hql, null);
		return (new PageModule(pageNumber,pageSize,this.getDataCountByAorB(num),objects));
	}

	public int getDataCountByAorB(Integer num) {
		String chql = "select count(*) from ZsApplyView where  1=1";
		if (null!=num) {
			chql+=" and ((CASE WHEN inYwdd = 'A' or inYwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inWydd = 'A' or inWydd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSxdd = 'A' or inSxdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inZzdd = 'A' or inZzdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inLsdd = 'A' or inLsdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inDldd = 'A' or inDldd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inWldd = 'A' or inWldd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inHxdd = 'A' or inHxdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSwdd = 'A' or inSwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inJsdd = 'A' or inJsdd = 'B' THEN 1 ELSE 0 END)) >=  "+num+"  ";
		}
/*		if (null!=num) {
			chql+=" and ((CASE WHEN inYwdd = 'A' or inYwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inWydd = 'A' or inWydd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSxdd = 'A' or inSxdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inZzdd = 'A' or inZzdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inLsdd = 'A' or inLsdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inDldd = 'A' or inDldd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inWldd = 'A' or inWldd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inHxdd = 'A' or inHxdd = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN inSwdd = 'A' or inSwdd = 'B' THEN 1 ELSE 0 END) + (CASE WHEN xyXxjs = 'A' or xyXxjs = 'B' THEN 1 ELSE 0 END)" +
			" + (CASE WHEN xyTyjs = 'A' or xyTyjs = 'B' THEN 1 ELSE 0 END) + (CASE WHEN xyZxzh = 'A' or xyZxzh = 'B' THEN 1 ELSE 0 END) + (CASE WHEN inJsdd = 'A' or inJsdd = 'B' THEN 1 ELSE 0 END)) >=  "+num+"  ";
		}*/

		int count = dao.findCountByHql(chql, null);
		return count;
	}
	/*
	 * public String checkSend(ZsUser user) {//检查申请表所需信息是否填写完整 ZsSchool school = new
	 * ZsSchool(); school =
	 * (ZsSchool)zsManager.getDataByInstance(1,1,null,null,school).getObjects().get(
	 * 0); StringBuffer message = new StringBuffer();
	 *
	 * ZsUserinfo userinfo=new ZsUserinfo(); userinfo.setZsUser(user);
	 * if(ufmanager.getDataByInstance(1,1,null,null,userinfo).getTotal()==0) {
	 * message.append("考生信息尚未填写,"); } else { userinfo =
	 * (ZsUserinfo)ufmanager.getDataByInstance(1,1,null,null,userinfo).getObjects().
	 * get(0); if(Status.ON.getStatus().equals(school.getScGrcsActive()) && (null ==
	 * userinfo.getInTc() || userinfo.getInTc().trim().length()==0) ) {
	 * message.append("考生个人陈述尚未填写,"); } }
	 *
	 * ZsCj cj=new ZsCj(); cj.setZsUser(user);
	 * if(Status.ON.getStatus().equals(school.getScCjActive()) &&
	 * cjmanager.getDataByInstance(1,1,null,null,cj).getTotal()==0) {
	 * message.append("高中成绩尚未填写,"); }
	 *
	 * ZsXy xy=new ZsXy(); xy.setZsUser(user);
	 * if(xymanager.getDataByInstance(1,1,null,null,xy).getTotal()==0) {
	 * message.append("学业水平综合测试尚未填写,"); }
	 *
	 * ZsExValue exvalue=new ZsExValue(); exvalue.setZsUser(user);
	 * if(Status.ON.getStatus().equals(school.getScExvalueActive()) &&
	 * exmanager.getDataByInstance(1,1,null,null,exvalue).getTotal()==0) {
	 * message.append("报考信息尚未填写,"); }
	 *
	 * return message.toString(); }
	 */

	public List getDataCountByHql (String column, String sendStatus, String appStatus, String checkStatus, String passStatus) {

		String hql = "select "+column+", count("+column+") from ZsApplyView where 1=1 ";
		if(null!=sendStatus&&sendStatus.trim().length()>0) {
			hql+=" and sendStatus='"+sendStatus+"' ";
		}
		if(null!=appStatus&&appStatus.trim().length()>0) {
			hql+=" and appStatus='"+appStatus+"' ";
		}
		if(null!=checkStatus&&checkStatus.trim().length()>0) {
			hql+=" and checkStatus='"+checkStatus+"' ";
		}
		if(null!=passStatus&&passStatus.trim().length()>0) {
			hql+=" and passStatus='"+passStatus+"' ";
		}
		hql+=" group by "+column;
		List<Object[]> list = dao.findByHql(1, 1000, hql, null);
		List objlist = new ArrayList();
		for(Object[] object : list){
            DataCount count = new DataCount();
            count.setName((String)object[0]);
            count.setValue(((Long)object[1]).intValue());
            objlist.add(count);
        }
		System.out.println(hql);
		return objlist;
	}

/*
	public String checkSend(ZsUser user) {//检查申请提交所需信息是否填写完整
		ZsSchool school = new ZsSchool();
		school = (ZsSchool)zsManager.getDataByInstance(1,1,null,null,school).getObjects().get(0);
		String message = this.checkApp(user);

		if(!Status.APPSTATUS_YES.getStatus().equals(smanager.getDataByUser(user).getAppStatus())) {
			message+="申请表尚未确认,";
		}

		List fnlist = anconfig.getConfigsInfoByClass("上传材料名称(必需)");
	    for (int i=0; i<fnlist.size(); i++){
	    	AnConfig fn = (AnConfig)fnlist.get(i);
	    	ZsFile file = new ZsFile();
	    	file.setZsUser(user);
	    	file.setFiName(fn.getCoValue());	
	    	List flist = fmanager.getDataByInstance(1,1,null,null,file).getObjects();
	    	if (flist.size()<1) {
	    		message+="材料"+fn.getCoValue()+"尚未上传,";
	    	}
	    }
		return message;
	}*/
}
